<?php
    class Blog extends Control {
	var $PageJump;
	var $CurrentPage;
	var $DiscussionData;
	var $DiscussionDataCount;
        var $RowNumber;
        var $Body;
        var $FormatType;
        var $ShowHtml;

        function Blog(&$Context) {
	    $this->Name = 'Blog';
	    $this->Control($Context);

            $DiscussionManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 'DiscussionManager');
	    $this->DiscussionData = false;
	    $this->DiscussionDataCount = false;
            $this->CurrentPage = ForceIncomingInt("page", 1);

            if (!$this->DiscussionData) {
		$this->DiscussionData = $DiscussionManager->GetDiscussionList($this->Context->Configuration['BLOG_PER_PAGE'], $this->CurrentPage, $this->Context->Configuration['BLOG_CATEGORY_ID']);
		$this->DiscussionDataCount = $DiscussionManager->GetDiscussionCount($this->Context->Configuration['BLOG_CATEGORY_ID']);
            }

            $this->RowNumber = 0;

            $this->CallDelegate('Constructor');
	}

	function Render() {
	    $this->CallDelegate('PreRender');
            $Draft = ForceIncomingInt('Draft', 0);
            $CategoryID = ForceIncomingInt('CategoryID', 0);
	    if ($CategoryID == 0) $CategoryID = '';
            $pl = $this->Context->ObjectFactory->NewContextObject($this->Context, 'PageList', 'CategoryID', $CategoryID);
	    $pl->NextText = $this->Context->GetDefinition('Next');
	    $pl->PreviousText = $this->Context->GetDefinition('Previous');
	    $pl->CssClass = 'PageList';
	    $pl->TotalRecords = $this->DiscussionDataCount;
	    $pl->CurrentPage = $this->CurrentPage;
	    $pl->RecordsPerPage = $this->Context->Configuration['BLOG_PER_PAGE'];
	    $pl->PagesToDisplay = 10;
	    $pl->PageParameterName = 'page';
	    $pl->DefineProperties();
	    $PageDetails = $pl->GetPageDetails($this->Context);
	    $PageList = $pl->GetNumericList();
            $PERMISSION_EDIT_DISCUSSIONS = $this->Context->Session->User->Permission('PERMISSION_EDIT_DISCUSSIONS');
            $PERMISSION_EDIT_COMMENTS = $this->Context->Session->User->Permission('PERMISSION_EDIT_COMMENTS');
            echo '<div class="ContentInfo Top"><h1>'.$this->Context->PageTitle.'</h1>
                <div class="PageInfo">
                	<p>'.($PageDetails == '' ? $this->Context->GetDefinition('NoDiscussionsFound') : $PageDetails).'</p>
                	'.$PageList.'
                </div></div>';
            echo '<div id="ContentBody"><ol id="BlogList">';

            $Discussion = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Discussion');
            $FirstRow = 1;
            $CurrentUserJumpToLastCommentPref = $this->Context->Session->User->Preference('JumpToLastReadComment');
            $BlogList = '';
            $ThemeFilePath = $this->Context->Configuration['EXTENSIONS_PATH'].'Blog/theme/Theme.Blog.php';
            $Alternate = 0;
            while ($Row = $this->Context->Database->GetRow($this->DiscussionData)) {
                $this->RowNumber++;
                $Discussion->Clear();
                $Discussion->GetPropertiesFromDataSet($Row, $this->Context->Configuration);
                $Discussion->FormatPropertiesForDisplay();
                $sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
                $sql->SetMainTable('Comment','u');
                $sql->AddSelect(array('Body', 'FormatType'), 'u');
                $sql->AddWhere('u','DiscussionID','',$Discussion->DiscussionID,'=');
                $result = $this->Context->Database->Select($sql, $this->Name, 'GetCommentBody', 'An error occurred while fetching the Comment Body information.');
                $rows = $this->Context->Database->GetRow($result);
                $CommentBody = get_main($rows['Body']);
                $CommentBody['main'] = $this->Context->FormatString($CommentBody['main'], $this, $rows['FormatType'], FORMAT_STRING_FOR_DISPLAY);
                include($ThemeFilePath);
                $FirstRow = 0;
                $Alternate = FlipBool($Alternate);
            }
            echo $BlogList.'</ol></div>';
            if ($this->DiscussionDataCount > 0) {
                echo '<div class="ContentInfo Bottom">
		<div class="PageInfo">
			<p>'.$pl->GetPageDetails($this->Context).'</p>
			'.$PageList.'
		</div>
		<a id="TopOfPage" href="'.GetRequestUri().'#pgtop">'.$this->Context->GetDefinition('TopOfPage').'</a>
                </div>';
            }
	}

    }
?>
